//
//  HaveRecBaseClass.m
//
//  Created by 春程 王 on 16/8/3
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "HaveRecBaseClass.h"
#import "HaveRecCookie.h"
#import "HaveRecJPieces.h"


NSString *const kHaveRecBaseClassResult = @"result";
NSString *const kHaveRecBaseClassCookie = @"cookie";
NSString *const kHaveRecBaseClassJPieces = @"jPieces";
NSString *const kHaveRecBaseClassWorkerId = @"workerId";


@interface HaveRecBaseClass ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation HaveRecBaseClass

@synthesize result = _result;
@synthesize cookie = _cookie;
@synthesize jPieces = _jPieces;
@synthesize workerId = _workerId;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.result = [[self objectOrNilForKey:kHaveRecBaseClassResult fromDictionary:dict] doubleValue];
            self.cookie = [HaveRecCookie modelObjectWithDictionary:[dict objectForKey:kHaveRecBaseClassCookie]];
    NSObject *receivedHaveRecJPieces = [dict objectForKey:kHaveRecBaseClassJPieces];
    NSMutableArray *parsedHaveRecJPieces = [NSMutableArray array];
    if ([receivedHaveRecJPieces isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedHaveRecJPieces) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedHaveRecJPieces addObject:[HaveRecJPieces modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedHaveRecJPieces isKindOfClass:[NSDictionary class]]) {
       [parsedHaveRecJPieces addObject:[HaveRecJPieces modelObjectWithDictionary:(NSDictionary *)receivedHaveRecJPieces]];
    }

    self.jPieces = [NSArray arrayWithArray:parsedHaveRecJPieces];
            self.workerId = [self objectOrNilForKey:kHaveRecBaseClassWorkerId fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithDouble:self.result] forKey:kHaveRecBaseClassResult];
    [mutableDict setValue:[self.cookie dictionaryRepresentation] forKey:kHaveRecBaseClassCookie];
    NSMutableArray *tempArrayForJPieces = [NSMutableArray array];
    for (NSObject *subArrayObject in self.jPieces) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForJPieces addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForJPieces addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForJPieces] forKey:kHaveRecBaseClassJPieces];
    [mutableDict setValue:self.workerId forKey:kHaveRecBaseClassWorkerId];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.result = [aDecoder decodeDoubleForKey:kHaveRecBaseClassResult];
    self.cookie = [aDecoder decodeObjectForKey:kHaveRecBaseClassCookie];
    self.jPieces = [aDecoder decodeObjectForKey:kHaveRecBaseClassJPieces];
    self.workerId = [aDecoder decodeObjectForKey:kHaveRecBaseClassWorkerId];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeDouble:_result forKey:kHaveRecBaseClassResult];
    [aCoder encodeObject:_cookie forKey:kHaveRecBaseClassCookie];
    [aCoder encodeObject:_jPieces forKey:kHaveRecBaseClassJPieces];
    [aCoder encodeObject:_workerId forKey:kHaveRecBaseClassWorkerId];
}

- (id)copyWithZone:(NSZone *)zone
{
    HaveRecBaseClass *copy = [[HaveRecBaseClass alloc] init];
    
    if (copy) {

        copy.result = self.result;
        copy.cookie = [self.cookie copyWithZone:zone];
        copy.jPieces = [self.jPieces copyWithZone:zone];
        copy.workerId = [self.workerId copyWithZone:zone];
    }
    
    return copy;
}


@end
